package com.kaifamiao.base.Demo210916;

import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @author lucien-ma
 * @date 2021/9/16
 * @apiNote
 */
public class Demo01 {

    public static void main(String[] args) {
        Person randomPerson = PersonFactory.getRandomPerson();
        Person randomPerson1 = PersonFactory.getRandomPerson();
        System.out.println(randomPerson);
        System.out.println(randomPerson1);
        System.out.println("+++++++++++++++++++++++++++++++");
        boolean b = randomPerson.canMarry(randomPerson1);
        System.out.println(b);
    }

}

/**
 * 人类
 */
@Data
class Person{
    private String name;
    private int age;
    private char gender;
    private boolean isMarry;

    public boolean canMarry(Person person) {
        if (this.gender == person.gender) {
            System.out.println("同性之间不能结婚");
            return false;
        }
        if (this.isMarry || person.isMarry) {
            System.out.println("已婚人士不能再结婚");
            return false;
        }
        if (!(this.ageIsMatch(this.gender, this.age) && person.ageIsMatch(this.gender, this.age))) {
            System.out.println("双方必须达到合法结婚年龄");
            return false;
        }
        System.out.println(this.name + "和" + person.name + "符合结婚条件");
        return true;
    }

    /**
     * 结合性别判断年龄是否到达已婚合法年龄
     * @return
     */
    private boolean ageIsMatch(char gender, int age) {
        boolean result = false;
        switch (gender) {
            case '男':
                if (age >= 22)
                    result = true;
                break;
            case '女':
                if (age >= 20)
                    result = true;
                break;
        }
        return result;
    }

}

/**
 * 用来随机生成一个 Person 对象
 */
class PersonFactory {
    private static List<String> nameList = new ArrayList<>();
    private static Random random = new Random();

    private PersonFactory(){}

    static {
        nameList.addAll(Arrays.asList("张三","李四","小明","小白","小黑","王五"));
    }
    public static Person getRandomPerson() {
        Person person = new Person();
        // 设置名字
        person.setName(nameList.get(random.nextInt(nameList.size()-1)));
        // 设置年龄
        person.setAge(random.nextInt(60)+1);
        // 设置性别
        if (random.nextBoolean()) {
            person.setGender('男');
        } else {
            person.setGender('女');
        }
        // 设置是否已婚
        person.setMarry(random.nextBoolean());
        return person;
    }
}
